﻿using System.Windows.Forms;
using BarCode.AccessDao;
using System.Collections.Generic;
using BarCode.Bean;
using BarCode.Common;

namespace BarCode.Forms
{
    public partial class Login : Form
    {
        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(Login));

        public Login()
        {
            InitializeComponent();
        }

        private void Login_Load(object sender, System.EventArgs e)
        {
            DB01Dao db01 = new DB01Dao(GlobalVal.serverMysqlInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
            List<UserBean> lstUser = db01.getUserInfoList(GlobalVal.Line_No);
            if (lstUser.Count == 0)
            {
                MessageBox.Show("尚未添加操作员！");
                return;
            }
            else
            {
                cmbUser.DataSource = lstUser;
                cmbUser.DisplayMember = "User";
                cmbUser.ValueMember = "User";
            }
        }

        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            // 退出系统
            Application.Exit();
        }

        private void cmbUser_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            txtPwd.Text = "";
        }

        private void btnOK_Click(object sender, System.EventArgs e)
        {

            DB01Dao db01 = new DB01Dao(GlobalVal.serverMysqlInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
            UserBean loginUser = db01.getUserInfo(GlobalVal.Line_No, cmbUser.SelectedValue.ToString());
            if (loginUser == null)
            {
                MessageUtil.Warn("操作员未注册！");
                txtPwd.SelectAll();
                txtPwd.Focus();
                return;
            }
            else
            {
                if ("".Equals(loginUser.Password) || loginUser.Password == null)
                {
                    if (txtPwd.Text == "")
                    {
                        GlobalVal.CurrentUser = loginUser;
                        if (!ShowForm(loginUser.lineno))
                        {
                            return;
                        }
                    }
                    else
                    {
                        MessageUtil.Warn("密码不正确，请重新输入！");
                        txtPwd.SelectAll();
                        txtPwd.Focus();
                        return;
                    }
                }
                else
                {
                    if (txtPwd.Text.Equals(loginUser.Password)) 
                    {
                        GlobalVal.CurrentUser = loginUser;
                        if (!ShowForm(loginUser.lineno))
                        {
                            return;
                        }
                    }
                    else
                    {
                        MessageUtil.Warn("密码不正确，请重新输入！");
                        txtPwd.SelectAll();
                        txtPwd.Focus();
                        return;
                    }
                }
            }
            this.Hide();
        }

        private bool ShowForm(string lineNo)
        {
            bool ret = true;
            //switch (lineNo)
            //{
            //    case "0001":
            //        Base_BarCode baseBarCodeForm = new Base_BarCode();
            //        baseBarCodeForm.Show();
            //        break;
            //    case "0003":
            //        JXJK_BarCode jxjkBarCode = new JXJK_BarCode();
            //        jxjkBarCode.Show();
            //        break;
            //    case "0005":
            //        frmSelect objFrmSelect = new frmSelect();
            //        objFrmSelect.Show();
            //        break;
            //    case "0007":
            //        MainBarCode jkBarCode = new MainBarCode();
            //        jkBarCode.Show();
            //        break;
            //    case "0":
            //        Config configForm = new Config();
            //        configForm.Show();
            //        break;
            //    default:
            //        MessageUtil.Warn("不存在对应的生产线编号！");
            //        ret = false;
            //        break;
            //}

            MainBarCode mainForm = new MainBarCode();
            mainForm.Show();
            return ret;
        }

    }


    
}
